<?php

namespace IDock\Insurance\ZYIC\Response;

use IDock\Http\Response;
use IDock\Insurance\ZYIC\Response\AllZYICResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\NonCar\NonCarProductListPrinter;
use IDock\Printer\NonCar\NonCarProductPrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Printer\UnderwritingRecallPrinter;

class NonCarInquiryResponse extends AllZYICResponse
{
    public $channelOrderNo = '';

    public function resolve(): Response
    {
        $body = $this->getBody();
        if (!json_decode($body)) {
            $this->setError('返回数据异常');
            return  $this;
        }
        $data = json_decode($body,true);
        if ($this->printer and  is_string($this->printer)) {
            $this->printer = new $this->printer;
        }

        $resultCode = $data['status'] ?? '';
        $resultMess = $data['message'] ?? '';

        if($resultCode != 200) {
            $this->setError($resultMess);
            return $this;
        }
        $this->setBody($data);

        return $this;
    }


    public function printerInit(): Response
    {
        $body = $this->getBody();
        $code = $body['msgCode'] ?? '';
        if($code != '0'){
            $this->setError($body['msgInfo'] ?? '');
        }
        $data = $body['data'] ?? [];
        $this->channelOrderNo = $data['channelOrderNo'] ?? '';
        $printer = new nonCarInquiryPrinter();
        $printer->totalPremium = $data['sumPremium'] ?? '0';
        foreach ($data['calcList'] as $val){
            foreach ($val['plan'] as $value){
                $plan = new NonCarInquirySinglePrinter();
                $plan->planCode = $value['planCode'] ?? '';
                $plan->premium = $value['premium'] ?? '';
                $printer->noCarInquirySingleAppend($plan);
            }
        }
        $this->printer = $printer;
        return $this;
    }
}
